package src.textReader;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: NRG
 * Date: 30.10.12
 * Time: 22:30
 */
public class TextReader {
    public TextReader() {
    }

    public LinkedHashMap<String, Integer> getLinkedHashMapFromFile(String fileName) {
        String myText = readTextFromFile(fileName);
        if (myText == null)
            return null;
        return getLinkedHashMapFromText(myText);
    }

    public LinkedHashMap<String, Integer> getLinkedHashMapFromText(String text) {
        ArrayList<String> myWords = parseInputText(text);
        LinkedHashMap<String, Integer> result = convertArrayListToLinkedHashMap(myWords);
        return result;
    }

    private String readTextFromFile(String fileName) {
        StringBuffer sb = new StringBuffer("");
        try {
            BufferedReader br = getFileBufferedReader(fileName);
            if (br == null) return null;
            String s;
            while ((s = br.readLine()) != null) {
                sb.append("\n" + s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    private BufferedReader getFileBufferedReader(String fileName) throws FileNotFoundException {
        File file = new File(fileName);
        if (!fileIsOk(file)) {
            return null;
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        return br;
    }

    private LinkedHashMap<String, Integer> convertArrayListToLinkedHashMap(List<String> words) {
        LinkedHashMap<String, Integer> result = new LinkedHashMap<String, Integer>();
        for (String x : words) {
            if(result.containsKey(x)){
                int val = result.get(x);
                result.remove(x);
                result.put(x,val+1);
                continue;
            }
            result.put(x, 1);
        }
        return result;
    }

    private ArrayList<String> parseInputText(String str) {
        ArrayList<String> result = new ArrayList<String>();
        str = str.toLowerCase();
        str = replaceUnnecessaryChars(str);
        String[] tmp = str.split("\\s+");
        for (String x : tmp) {
            if (isInteger(x)) {
                continue;
            }
            result.add(x);
        }
        return result;
    }

    private String replaceUnnecessaryChars(String s) {
        String str = new String(s);
        char[] unnecessary = {'.', ',', '&', '$', '!', ';', ':', '%', '@', '#',
                '^', '&', '*', '/', '\\', '{', '}', '[', ']', '(', ')', '+',
                '-', '"', '~', '<', '>', '.'};
        for (char x : unnecessary) {
            str = str.replace(x, ' ');
        }
        return str;
    }

    private boolean fileIsOk(File file) {
        if (file.exists() && file.isFile()) {
            return true;
        }
        return false;
    }

    private boolean isInteger(String s) {
        Pattern pt = Pattern.compile("\\d*([.,])*\\d*");
        Matcher mc = pt.matcher(s);
        return mc.matches();
    }
}
